package cn.com.yto56.coresystem.business.msg.soa.biz.impl;

import cn.com.yto56.coresystem.business.msg.soa.biz.IUserInfoBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.db.IPage;
import cn.com.yto56.coresystem.common.msg.util.db.QueryPage;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IUserGroupInfoDao;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IUserInfoDao;
import cn.com.yto56.coresystem.logic.msg.soa.domain.UserGroupInfoBean;
import cn.com.yto56.coresystem.logic.msg.soa.domain.UserInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by wangrui on 2017/2/16.
 */
@Service
public class UserInfoBizImpl implements IUserInfoBiz {
    @Autowired
    private IUserInfoDao userInfoDaoImpl;
    @Autowired
    private IUserGroupInfoDao userGroupInfoDaoImpl;


    /**
     * 外部系统注册新用户
     *
     * @param sysInfo_Id   appId对应系统id
     * @param sys_userId   外部系统注册的userId
     * @param sys_userName 外部系统注册的用户名
     * @param sys_remark   外部系统注册用户是的注释
     * @return 消息中心分配的userId
     */
    @Override
    @Transactional
    public AppResult registerUserInfo(String sysInfo_Id, String sys_userId, String sys_userName, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(sys_userId)) {
            resultMessage = "参数[yto_msg_sys_userId]不能为空";
        } else if (StringUtils.isBlank(sys_userName)) {
            resultMessage = "参数[yto_msg_sys_userName]不能为空";
        } else {
            UserInfoBean userInfoBean = new UserInfoBean();
            userInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
            userInfoBean.setYto_msg_sys_userid(sys_userId);
            userInfoBean.setStatus("VALID");
            UserInfoBean checkUpdate = userInfoDaoImpl.checkAndGetValue(userInfoBean);
            if (checkUpdate != null && checkUpdate.getYto_msg_userid() != null) {
                datas = new HashMap();
                datas.put("yto_msg_userId", checkUpdate.getYto_msg_userid());
                resultMessage = "用户" + sys_userId + "已经注册过";

            } else {
                String userId = "MSG_USER_" + UUID.randomUUID().toString();
                datas = new HashMap();
                datas.put("yto_msg_userId", userId);
                userInfoBean.setYto_msg_userid(userId);
                userInfoBean.setYto_msg_sys_username(sys_userName);
                userInfoBean.setYto_msg_sys_remark(sys_remark);
                userInfoBean.setCreate_time(new Date());
                int t = userInfoDaoImpl.add(userInfoBean);
                if (t <= 0) {
                    resultMessage = "服务器错误-数据保存失败";
                } else {
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    /**
     * 删除用户
     *
     * @param sysInfo_Id appId对应系统id
     * @param userId     消息中心用户id
     * @param status     删除状态  true安全删除 false强制删除
     * @param sys_remark 删除注释
     * @return
     */
    @Override
    @Transactional
    public AppResult deleteUserInfo(String sysInfo_Id, String userId, String status, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "[系统异常]-sql执行异常";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else {
            if (StringUtils.isBlank(status)) {
                status = "0";
            }
            //安全删除
            if (status.equals("0")) {
                //1.检查该用户在组中是否存在
                UserGroupInfoBean userGroupInfoBean = new UserGroupInfoBean();
                userGroupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                userGroupInfoBean.setYto_msg_userid(userId);
                userGroupInfoBean.setStatus("VALID");
                boolean checkUpdate = userGroupInfoDaoImpl.searchForUpdate(userGroupInfoBean);
                if (!checkUpdate) {
                    resultMessage = "该用户关联其他分组，不能删除,如果强制删除请使用强制删除接口";
                } else {
                    UserInfoBean where = new UserInfoBean();
                    where.setYto_msg_sysinfo_id(sysInfo_Id);
                    where.setYto_msg_userid(userId);
                    UserInfoBean set = new UserInfoBean();
                    set.setStatus("INVALID");
                    set.setYto_msg_sys_remark(sys_remark);
                    set.setUpdate_time(new Date());
                    int t = userInfoDaoImpl.update(set, where);
                    if (t <= 0) {
                        resultMessage = "[系统异常]sql执行失败";
                    } else {
                        resultFlag = true;
                        resultMessage = "删除成功";
                    }

                }

            } else {
                //1.删除userinfo
                UserInfoBean userInfoBean = new UserInfoBean();
                userInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                userInfoBean.setYto_msg_userid(userId);
                userInfoBean.setYto_msg_sys_remark(sys_remark);
                int t = userInfoDaoImpl.delete(userInfoBean);
                //2.删除usergroup中的信息
                UserGroupInfoBean userGroupInfoBean = new UserGroupInfoBean();
                userGroupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                userGroupInfoBean.setYto_msg_userid(userId);
                t = userGroupInfoDaoImpl.delete(userGroupInfoBean);
                //3.删除代发列表中的相关userid
                if (t > 0) {
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            }
        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult checkUser(String sysInfo_Id, String userId) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "[系统异常]-sql执行异常";
        Map datas = null;
        UserInfoBean userInfoBean = new UserInfoBean();
        userInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
        userInfoBean.setYto_msg_userid(userId);
        IPage<UserInfoBean> userInfoBeanIPage = userInfoDaoImpl.search(userInfoBean, new QueryPage(20, 1));
        List<UserInfoBean> userInfoBeanList = (List<UserInfoBean>) userInfoBeanIPage.getRows();
        if (userInfoBeanList != null && userInfoBeanList.size() > 0) {
            datas = new HashMap();
            datas.put("yto_msg_sysInfo_id", sysInfo_Id);
            datas.put("yto_msg_userId", userId);
            resultFlag = true;
            resultMessage = "校验成功";
        } else {
            resultMessage = "该用户不存在";
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }


}
